package com.java.base.exercise;

import com.java.base.exercise.current.threadpool.SingleThreadPoolDemo;

import java.math.BigDecimal;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.locks.StampedLock;

/**
 * @Author RenPu
 * @Date 2022/8/17 17:32
 * @Version 1.0
 * @Description:
 **/
public class TestAc {

    public static void main(String[] args) throws InterruptedException {
//        BigDecimal total=BigDecimal.ZERO;
//        System.out.println(total.toString());
//
//        for (int i = 0; i < 10; i++) {
//            BigDecimal bigDecimal1=new BigDecimal("10");
//
//            total=total.add(bigDecimal1);
//        }
////        total=new BigDecimal("0.00");
//        total=new BigDecimal("-100");
//        if(total.compareTo(BigDecimal.ZERO)==1){
//            System.out.println("逻辑正确。。。。。。");
//        }
//        System.out.println(total.toString());


        for (int i = 0; i <10; i++) {

            if(i%2!=0){
                System.out.println("正常逻辑处理。。。。。。。。"+i);

            }else if(i%2==0){
                System.out.println("特殊逻辑的处理。。。。。。。。"+i);
                continue;
            }



        }


        updateAndRead();

    }


    public static void updateAndRead() throws InterruptedException {


        /**
         * 累加器的使用
         */
        ExecutorService executor = Executors.newFixedThreadPool(10);

        LongAccumulator balanace = new LongAccumulator(Long::sum,1000);
        Runnable runnable=()->{
            balanace.accumulate(1000L);
        };

        for (int i = 0; i <50 ; i++) {
            executor.submit(runnable);
        }

        executor.shutdown();

        if (executor.awaitTermination(1000L, TimeUnit.MILLISECONDS))
            System.out.println("Balance: " + balanace.get());
        assert balanace.get() == 51000L;


//        /**
//         * 锁的使用
//         */
//        StampedLock lock = new StampedLock();
//        People people = new People();
//        people.setAge(10000);
//        Runnable w = () -> {
//            long stamp = lock.writeLock();
//            people.setAge(people.getAge()+1000);
//            System.out.println("Write  Age:{       }"+people.getAge());
//            lock.unlockWrite(stamp);
//        };
//
//
//        Runnable r = () -> {
//            long stamp = lock.tryOptimisticRead();  //尝试获取乐观锁标识
//            if(!lock.validate(stamp)){              //验证锁标识
//                try{
//                    stamp= lock.readLock();          //获取读锁
//                    System.out.println("Read ....Age:{  }"+people.getAge());
//                }finally {
//                    lock.unlockRead(stamp);           //释放锁
//                }
//            }else {
//
//                System.out.println("Optimistic read fails");
//
//            }
//
//
//
//        };
//
//
//
//        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
//        for (int i = 0; i <50 ; i++) {
//            fixedThreadPool.submit(w);
//            fixedThreadPool.submit(r);
//        }
//
//
//        fixedThreadPool.shutdown();
//
//
   }




}
